package com.wtb.listener;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wtb.pub.UnCachException;
import com.wtb.qq.domain.QqOauth;
import com.wtb.rr.domain.RrOauth;
import com.wtb.thread.CountWriteThread;
import com.wtb.thread.QqReadThread;
import com.wtb.thread.QqTraverThread;
import com.wtb.thread.QqWriteThread;
import com.wtb.thread.RrReadThread;
import com.wtb.thread.RrTraverThread;
import com.wtb.thread.RrWriteThread;

public class ThreadListener implements ServletContextListener {
	private static final Log log = LogFactory.getLog(ThreadListener.class);
	private BlockingQueue<QqOauth> qqQueue = new ArrayBlockingQueue<QqOauth>(20);
	private BlockingQueue<RrOauth> rrQueue = new ArrayBlockingQueue<RrOauth>(20);
	private BlockingQueue<RrOauth> traver_rrQueue = new LinkedBlockingDeque<RrOauth>();
	private BlockingQueue<QqOauth> traver_qqQueue = new LinkedBlockingDeque<QqOauth>();

	public void contextDestroyed(ServletContextEvent sce) {
		
	}
	public void contextInitialized(ServletContextEvent sce) {
		log.info("线程监听器启动！！！");
		Runnable rrTraveRunnable = new RrTraverThread(traver_rrQueue);
		Thread rrTrvaerThread = new Thread(rrTraveRunnable,"人人遍历线程");
		rrTrvaerThread.setUncaughtExceptionHandler(new UnCachException(rrTraveRunnable));
		rrTrvaerThread.start();
		
		Runnable qqTraveRunnable = new QqTraverThread(traver_qqQueue);
		Thread qqTraverThread = new Thread(qqTraveRunnable,"QQ遍历线程");
		qqTraverThread.setUncaughtExceptionHandler(new UnCachException(qqTraveRunnable));
		qqTraverThread.start();
		
		Runnable countWriteRunnable = new CountWriteThread();
		Thread countWriteThread= new Thread(countWriteRunnable,"数量记录线程");
		countWriteThread.setUncaughtExceptionHandler(new UnCachException(countWriteRunnable));
		countWriteThread.start();

		/*if (Config.MAIL_RECEIVERS != null) {
			Runnable mailSendRunnable = new MailSendThread();
			Thread mailSendThread = new Thread(mailSendRunnable,"邮件发送线程");
			mailSendThread.setUncaughtExceptionHandler(new UnCachException(mailSendRunnable));
			mailSendThread.start();
		}*/
		//分别创建3个线程进行读工作
		for (int i = 0; i < 5; i++) {
			Runnable rrReadRunnable = new RrReadThread(qqQueue,traver_rrQueue);
			Thread rrReadThread = new Thread(rrReadRunnable,"人人读线程"+i+i+i+i);
			rrReadThread.setUncaughtExceptionHandler(new UnCachException(rrReadRunnable));
			rrReadThread.start();
			
			Runnable qqReadRunnable = new QqReadThread(rrQueue,traver_qqQueue);
			Thread qqReadThread = new Thread(qqReadRunnable,"QQ读线程"+i+i+i+i);
			qqReadThread.setUncaughtExceptionHandler(new UnCachException(qqReadRunnable));
			qqReadThread.start();
		}
	
		Runnable rrWriteRunnable = new RrWriteThread(rrQueue);
		Thread rrWriteThread = new Thread(rrWriteRunnable,"人人写线程0000");
		rrWriteThread.setUncaughtExceptionHandler(new UnCachException(rrWriteRunnable));
		rrWriteThread.start();
		
		Runnable qqWriteRunnable = new QqWriteThread(qqQueue);
		Thread qqWriteThread = new Thread(qqWriteRunnable,"QQ写线程0000");
		qqWriteThread.setUncaughtExceptionHandler(new UnCachException(qqWriteRunnable));
		qqWriteThread.start();
		
		
	}

}
